#!/bin/bash

webName="daiwoniu-demo"
webPath="/opt/webapp"
webC=${webPath}"/"${webName}"/current"
webR=${webPath}"/"${webName}"/releases"
webS=${webPath}"/"${webName}"/shared"

if [ ! -d ${webC} ]; then
  mkdir -p ${webC}
fi
if [ ! -d ${webR} ]; then
  mkdir -p ${webR}
fi
if [ ! -d ${webS} ]; then
  mkdir -p ${webS}
fi
if [ ! -d ${webS}"/upload" ]; then
  mkdir -p ${webS}/upload
fi
if [ ! -d ${webS}"/logs" ]; then
  mkdir -p ${webS}/logs
fi

echo $webC
echo $webR
echo $webS

rm -f lib/*.jar target/*.tar.gz
mvn -Dmaven.test.skip=true clean package
VERSION=`ls target/*.tar.gz |awk 'BEGIN{FS="[-.]"} END{print $3}'`
echo $VERSION

rm -f ${webR}/*.tar.gz
cp target/*.tar.gz ${webR}/
cd ${webR}/
tar -xvzf ${webName}-${VERSION}.tar.gz

cd ${webC}/
if [  -f "bin/stop.sh" ]; then
  ./bin/stop.sh
fi
rm -rf ${webC}/*
mv ${webR}/${webName}-${VERSION}/* ${webC}/
rm -rf ${webR}/${webName}-${VERSION}
ln -s ${webS}/upload ${webC}/webapp/upload
ln -s ${webS}/logs ${webC}/logs

chmod -R 0775 ${webC}
./bin/start.sh
